<?php
include('../../__global.php');

class aj_repostSubmit extends ml_controller {

    private $uid;
    private $col_id;
    private $rid;
    private $comment;
    private $pic_id;
    private $col_info=array();
    private $feedrid;
    private $infoAndName=array();

    function initParam() {

        $this->uid=$this->__visitor['uid'];
        $this->col_id=$this->input('col_id','');
        $this->rid=$this->input('rid','');
        $this->comment=$this->input('comment','');
        $this->share2weibo=$this->input('share','');


    }

    function checkParam() {

        $this->check_referer();
        if ($this->check_permission(ML_PERMISSION_LOGOUT_ONLY)) {
            $this->api_output(ML_RCODE_NOLOGIN);
        }

        if ($this->check_permission(ML_PERMISSION_UNVERIFY_ONLY)) {
            $this->api_output(ML_RCODE_NOACTIVE);
        }
        if (empty($this->col_id)) {
            $this->api_output(ML_RCODE_PARAM,'','null col_id');
        }
        if (Tool_string::str_width($this->comment)>280) {
            $this->api_output(ML_RCODE_PARAM,'','comment too long');
        }
        if (!ml_tool_isuid::is_mluid($this->uid)) {
            $this->api_output(ML_RCODE_PARAM,'','wrong uid');
        }


    }

    public function main() {
            
        $modelRe=new ml_model_dbCollectionRe();
        $model=new ml_model_dbCollectionBox();
        $this->get_pic_id();
        $this->get_col_id($model);//其中的name用来插入评论表，作为冗余name
        $verifyData=$modelRe->verifyCollected($this->uid, $this->col_id, $this->rid);
        if(empty($verifyData)){
            $rs1=$modelRe->submitRepost($this->uid,$this->col_id,$this->rid,$this->comment);//向关系表中加一个记录
            $col_rel_id = $modelRe->insert_id();
            $this->send_user_feed($modelRe->insert_id());//我的分享表插feed
            
            $this->col_info=$this->infoAndName['col_info'];
            $this->col_info_factory();
            $rs2=$model->submitRepost($this->uid,$this->col_id,$this->col_info);//采集数+1&update专辑的前9个图片
            
            ml_tool_sendMq::atme_in_collect($this->uid , $this->rid , $col_rel_id);//投at队列
            
            
            $redis=new ml_model_rdsCount();
            $redis->incrColCnt($this->rid);//宝贝被转采数+1
            //正在考虑要不要用队列
            //~~~~~~~~~~~~~~~~~~~~~~~~~
            
            $comModel=new ml_model_dbCollectionComment();
            $comModel->insert_comment($this->rid, $this->infoAndName['name'], $this->uid, $this->col_id, $this->comment);
    
            //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


            $this->api_output(ML_RCODE_SUCC);
        }
        else{
            $this->api_output(ML_RCODE_FAIL,'','collected');
        }

    }
    private function col_info_factory(){
        
        $this->col_info=empty($this->col_info)?array():$this->col_info;
        if(count($this->col_info)>=9) array_pop($this->col_info);
        $this->col_info=array_merge(array($this->pic_id),$this->col_info);
        
    }

    private function send_user_feed($id) {
        
        
        $oGenerator = new ml_model_dbIdGenerator();
        $userfeed_res = $oGenerator->get_userfeed_rid($this->uid, $userfeed_rid);
        if(!$userfeed_res){
            $this->api_output(ML_RCODE_BUSY , '' , ML_RMESSAGE_BUSY);
        }
        $userfeed_data['uid'] =  $this->uid;
        $userfeed_data['content'] = $this->comment;
        $userfeed_data['rid'] = $userfeed_rid;
        $userfeed_data['ctime'] =  date('Y-m-d H:i:s', time());
        $userfeed_data['feed_info']=array('id'=>$id,'rid'=>$this->rid,'col_name'=>$this->infoAndName['name'],'col_id'=>$this->col_id);
        $userfeed_data['type'] = ML_FEED_TYPE_REPOST_COLLECTION;
        $userfeed_data['cip'] = Tool_ip::get_real_ip();
        $userfeed_data['pic_id']=$this->pic_id;
        $model_feed=new ml_model_dbUserfeed();
        $model_feed->insert_content($userfeed_data);
         
        
        $data = array('type'=>'repost','feed_rid'=>$userfeed_rid,'uid' => $this->uid,'nick'=>$this->__visitor['nick'],'content_rid'=>$this->rid,'comment'=>$this->comment,'col_name'=>$this->infoAndName['name'],'col_id'=>$this->col_id);
        ml_tool_sendMq::add_feed($data);
    }
    
    private  function get_pic_id(){
        $contentModel=new ml_model_dbContent();
        $conData=$contentModel->get_by_rid($this->rid);
        if($conData['status']=='9'||empty($conData))
        {
            $this->api_output(ML_RCODE_PARAM,'','wrong rid');
        }
        $this->pic_id=$conData['pic_id'];
    }
    private function get_col_id(&$model){
        $this->infoAndName=$model->getColinfo($this->uid, $this->col_id);
        if (empty($this->infoAndName['name'])) {
            $this->api_output(ML_RCODE_PARAM,'','wrong col_id');
        }

    }

}

new aj_repostSubmit();